package com.itheima.controller;

import com.itheima.pojo.Empl;
import com.itheima.pojo.Result;
import com.itheima.service.EmplService;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmplService emplService;

    //登陆模块
    @PostMapping("/login")
    public Result Login(@RequestBody  Empl e){
        log.info("请求登录:{},{}",e.getUsername(),e.getPassword());
        Empl empl=emplService.login(e.getUsername(),e.getPassword());
        //登陆成功,下发令牌
        if (empl!=null){
            Map<String, Object> claims=new HashMap<>();
            claims.put("id",empl.getId());
            claims.put("name",empl.getName());
            claims.put("username",empl.getUsername());
            String jwt = JwtUtils.getJwt(claims);
            return Result.success(jwt);
        }
        //登陆失败,返回错误信息
        return Result.error("login fail");
        //去掉令牌后,执行下面方法
        //return Result.success();
    }
}
